package cn.thoughtworks.school.usercenter.repositories;

import cn.thoughtworks.school.usercenter.entities.UserRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

public interface UserRoleRepository extends JpaRepository<UserRole, Long> {

  List<UserRole> findByUserIdIn(List<Long> ids);

  List<UserRole> findByOrganizationId(Long organizationId);

  List<UserRole> findByOrganizationIdAndUserIdIn(Long organizationId, List<Long> idList);

  UserRole findByOrganizationIdAndUserIdAndRole(Long organizationId, Long userId, Integer role);

  @Modifying
  @Transactional
  @Query("delete from UserRole where organizationId=?1 and userId=?2 and role=?3")
  void deleteByOrganizationIdAndUserIdAndRole(Long organizationId, Long userId, Integer role);

  List<UserRole> findByUserIdAndOrganizationId(Long userId, Long organizationId);

  Optional<UserRole> findByUserIdAndRole(Long userId, Integer organizationRole);

  List<UserRole> findByUserId(Long userId);

  List<UserRole> findByOrganizationIdAndRole(Long organizationId, Integer role);

  List<UserRole> findAllByUserId(Long userId);

}
